package com.example.tuter.activity;

import android.os.Bundle;
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.tuter.R;
import com.example.tuter.entity.Message;
import com.example.tuter.util.Constant;
import com.example.tuter.util.SPUtils;
import com.example.tuter.util.httpUtil.HttpCodeConstant;
import com.example.tuter.util.httpUtil.HttpFunction.UserAPI;

import retrofit2.Call;
import retrofit2.Callback;

public class UpdateUserInfoActivity extends BaseActivity {

    private UserAPI userAPI;
    private EditText editText;
    private Button button_update_username;
    private ImageView imageView_back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }


    @Override
    protected void initData() {
        userAPI = Constant.r.create(UserAPI.class);
    }

    @Override
    protected void bindEvent() {
        imageView_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        button_update_username.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String name = editText.getText().toString();
                updateUsername(name);
            }
        });
    }

    @Override
    protected void initView() {


        setContentView(R.layout.activity_update_user_info);

        editText = (EditText) findViewById(R.id.edittext_update_username);
        button_update_username = (Button) findViewById(R.id.button_activity_update_username);
        imageView_back = (ImageView) findViewById(R.id.activity_change_name_imageview);

    }

    @Override
    protected int getLayoutID() {
        return R.layout.activity_update_user_info;
    }

    /**
     * 更新用户昵称的函数
     * @param name
     */
    private void updateUsername(final String name) {
        Call<Message<String>> call = userAPI.updateUsername(name);
        call.enqueue(new Callback<Message<String>>() {
            @Override
            public void onResponse(Call<Message<String>> call, retrofit2.Response<Message<String>> response) {
                Message<String> message = response.body();
                if (message.getCode() != HttpCodeConstant.SUCCESS.getValue()) {
                    Looper.prepare();
                    String text = "更改失败,请重试" + call;
                    Toast.makeText(UpdateUserInfoActivity.this, text, Toast.LENGTH_SHORT).show();
                    Looper.loop();
                } else {
                    //更新ui
                    SPUtils.putUserInfo(UpdateUserInfoActivity.this, Constant.userInfo);
                    finish();
                    Looper.prepare();
                    Toast.makeText(UpdateUserInfoActivity.this, "更改成功", Toast.LENGTH_SHORT).show();
                    Looper.loop();
                }
            }

            @Override
            public void onFailure(Call<Message<String>> call, Throwable t) {

            }
        });
    }

}
